<?php
class inventory4excel extends BaseBean{
    
    public $_table= "inventory_temp";
    public $_primarykey= "id";
    

    public static function ClearExistsData($finDate,$conn){
        
        $conn->prepare("delete from result00..inventory_temp where datediff(m,finance_date,'".$finDate."')=0");
        $conn->execute();
    }


    public function __construct(){
        $this->defDbCfg["database"] = "result00";
    }

    public function insertItem() {
    try {
        $fields = array(
            "qty_7",
            "qty_8_15",
            "qty_16_30",
            "qty_31_60",
            "qty_61_90",
            "qty_90_120",
            "qty_120_150",
            "qty_150_365",
            "qty_365"
        );
        $periods = array(
            "qty_7" => "0,7",
            "qty_8_15" => "8,15",
            "qty_16_30" => "16,30",
            "qty_31_60" => "31,60",
            "qty_61_90" => "61,90",
            "qty_90_120" => "91,120",
            "qty_120_150" => "121,150",
            "qty_150_365" => "151,365",
            "qty_365" => "366"
        );
        $amount_fields = array(
            "qty_7" => "amount_7",
            "qty_8_15" => "amount_8_15",
            "qty_16_30" => "amount_16_30",
            "qty_31_60" => "amount_31_60",
            "qty_61_90" => "amount_61_90",
            "qty_90_120" => "amount_90_120",
            "qty_120_150" => "amount_120_150",
            "qty_150_365" => "amount_150_365",
            "qty_365" => "amount_365"
        );
        $quantity = $this->get("quantity");
        if ($quantity != 0) {
            $totalCost = $this->get("total_cost");
            if ($totalCost == null || $totalCost == "") {
                $totalCost = 0;
            }
            //$priceCost = $totalCost / $quantity;
            $priceCost = round($totalCost / $quantity,4);
            $subTotalCost =0;
		 
            $this->set("price_cost", $priceCost);
            for ($i = 0; $i < count($fields); $i++) {
                $start = "";
                $end = "";
                $field = $fields[$i];
                $amount_field = $amount_fields[$field];
                $finDate = $this->get("finance_date");
                $period = $periods[$field];
                $arr = explode(",", $period);
                $start = $arr[0];
                if (count($arr) > 1) {
                    $end = $arr[1];
                }
                if ($quantity <= 0) break;

                $sql = "select sum(ISNULL(rkdmx.sl,0)) as qty  from  
                ..rkdmx rkdmx inner join result00..rkd rkd on rkdmx.rkdno=rkd.no  
	 						where rkdmx.hh='" . $this->get("hh") . "' and rkd.no like 'SH%' and datediff(d,rkd.zdrq,'" . $finDate . "')>=" . $start;
                if ($end != "") {
                    $sql.= " and datediff(d,rkd.zdrq,'" . $finDate . "')<=" . $end;
                }
                $sql.= " group by rkdmx.hh";;
                $qty = $this->connection->fetchColumn($sql);
                if ($qty != null && $qty != "") {
                    $cost = 0;
                    if ($qty > $quantity) {
                        $qty = $quantity;
                    }
                    $quantity = $quantity - $qty;
                    if($quantity>0){
                        $cost = round($qty * $priceCost ,3);    
                        $subTotalCost =$subTotalCost+$cost;
                    }
                    else{ //如果是最后一次则用TotalCost -之前已经算出的成本总和

                        $cost = $totalCost - $subTotalCost;
                    }
                    $this->set($field, $qty);
                    $this->set($amount_field,$crt_cost);
                }
            }
        }
        parent::insertItem();
        //$id = $this->get("id");
	        
	    }
	    catch(Exception $ex) {
	        echo $ex->getMessage() . "</br>" . $ex->getTraceAsString();
	    }
	}

   
}
?>